<?php

namespace App\Http\Controllers;

use App\Helpers\ResponseEnum;
use App\Http\Requests\SaveUserInfoRequest;
use App\Models\Details;
use App\Models\Orders;
use App\Models\System;
use App\Models\Users;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class MyController extends BaseController
{
    public function index()
    {
        $userInfo = session('loginUser');

        $isCode = Users::where('id', $userInfo['id'])->value('is_qrcode');

        return view('my', ['data' => $userInfo, 'isCode' => $isCode]);
    }

    /**
     * @return
     * 我的订单列表
     */
    public function myOrder()
    {
        $wait = [];

        $waitSend = [];

        $waitOver = [];

        $datas = Orders::where('user_id', Arr::get(session('loginUser'), 'id'))->orderByDesc('id')->get(['order_sn', 'courier_sn', 'goods_name', 'goods_num', 'goods_cover', 'goods_prices', 'status', 'prices', 'total_price', 'user_name', 'user_mobile', 'shipping_address', 'pay_at', 'refund_at', 'created_at',]);


        if (!empty($datas->first())) {

            foreach ($datas as $key => $val) {
                //待付款
                if ($val->status == 1) {
                    $wait[] = $val;
                } elseif ($val->status == 2) { //待发货
                    $waitSend[] = $val;
                } elseif ($val->status == 3) { //待收货
                    $waitOver[] = $val;
                }
            }
        }
        return view('my/index', ['all' => $datas, 'wait' => $wait, 'waitSend' => $waitSend, 'waitOver' => $waitOver, 'reward' => System::where('id', 999)->value('reward')]);
    }

    /**
     * @return
     * 我的余额
     */
    public function myMoney()
    {

        $user = session('loginUser');

        //用户余额
        $userMoney = Users::where('id', $user['id'])->value('money');
        //用户余额明细
        $details = Details::where('user_id', $user['id'])->orderByDesc('id')->get(['id', 'user_id', 'money', 'status', 'created_at']);
        //待入账总金额
        $wait = 0;
        //提现总金额
        $take = 0;

        if ($details->first()) {
            foreach ($details as $value) {
                if ($value->status == 1) {
                    $wait += $value->money;
                } elseif ($value->status == 4) {
                    $take += $value->money;
                }
            }
        }

        //dd($money);

        return view('my/money', ['datas' => $details, 'money' => $userMoney, 'wait' => $wait, 'take' => $take]);
    }

    /**
     * 检测是否拥有分享分享权限
     */
    public function share()
    {
        $user = session('loginUser');

        $status = Users::where('id', $user['id'])->get(['is_qrcode', 'code'])->first();

        if ($status->is_qrcode == 1) {
            return $this->success(['code' => $status->code]);
        }

        return $this->fail(ResponseEnum::CLIENT_HTTP_NOT_SHARE);

    }


    /**
     * 我的邀请人
     */
    public function myInvitation(){
        $datas = Users::where('parent_user_id' , Arr::get(session('loginUser'), 'id'))->get(['name', 'avatar', 'phone' , 'created_at']);

        return view('my/invitation' , ['datas' => $datas]);
    }

    /**
     * 实名认证
     */
    public function authentication()
    {
        $user = Users::where('id', Arr::get(session('loginUser'), 'id'))->first();


        return view('my/authentication', ['name' => $user->name, 'mobile' => $user->phone, 'idCard' => $user->id_card]);
    }

    /**
     * @param SaveUserInfoRequest $request
     * 修改个人信息
     */
    public function saveUserInfo(SaveUserInfoRequest $request)
    {

        $loginUser = session('loginUser');

        $user = Users::where('id', $loginUser['id'])->first();

        if (!empty($user)) {
            $user->name = $request->post('name');

            $user->phone = $request->post('mobile');

            $user->id_card = $request->post('id_card');

            if ($user->save()) {
                return $this->success();
            }
        }

        return $this->fail(HTTP_ERROR);
    }
}
